package kbl.language;

import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map.Entry;

public class DFAState {
	// Binary tree, cached itself
	DFAState left;
	DFAState right;
	private TreeSet<NFAState> NFASet;
	private int id = 0;
	String name = new String();
	private boolean match = false;
	TreeMap<CharacterClass, DFAState> DFAMap = new TreeMap<CharacterClass, DFAState>();
	boolean marked = false;

	DFAState(StrategyNFA2DFA dfa) {
		this.setId(dfa.getDFAcount());
		dfa.incDFAcount();
	}

	DFAState(StrategyNFA2DFA dfa, TreeSet<NFAState> NFAs) {
		this(dfa);
		this.NFASet = NFAs;
		this.name = Integer.toString(this.id);
		for (NFAState nfa : NFAs) {
			if (nfa.isMatch()) {
				this.match = true;
				this.name = this.name.concat(nfa.toString());
			}
		}
	}

	DFAState match(CharRange cr) {
		for (Entry<CharacterClass, DFAState> entry : DFAMap.entrySet()) {
			CharacterClass key = entry.getKey();
			if (key.contains(cr))
				return entry.getValue();
		}
		return null;
	}

	boolean isMatch() {
		return this.match;
	}

	void setId(int id) {
		this.id = id;
	}

	int getId() {
		return id;
	}

	TreeSet<NFAState> getNFASet() {
		return NFASet;
	}

	@Override
	public String toString() {
		return id + NFASet.toString();
	}
}